package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponUser;
import com.cskaoyan.market.service.AdminCouponService;
import com.cskaoyan.market.service.AdminCouponServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@WebServlet("/admin/coupon/*")
public class AdminCouponController  extends HttpServlet {
    AdminCouponService adminCouponService=new AdminCouponServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op =requestURI.replace(req.getContextPath()+"/admin/coupon/", "");
        if("list".equals(op)){
            list(req,resp);
        }else{
            if("listuser".equals(op)){
                listUser(req,resp);
            }
        }
        if("read".equals(op)){
            read(req,resp);
        }
    }
    private void read(HttpServletRequest req, HttpServletResponse resp)  {

        String s = null;
        try {
            String id = req.getParameter("id");
            MarketCoupon couponList=adminCouponService.read(id);
            Object o = ResponseUtil.ok(couponList);
            s = JacksonUtil.getObjectMapper().writeValueAsString(o);
            resp.getWriter().println(s);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    private void listUser(HttpServletRequest req, HttpServletResponse resp)  {
        String s = null;
        try {
            String page = req.getParameter("page");
            String limit = req.getParameter("limit");
            String sort = req.getParameter("sort");
            String order = req.getParameter("order");
            String couponId = req.getParameter("couponId");
            String userId =req.getParameter("userId");
            String status = req.getParameter("status");
            List<MarketCouponUser> couponList=adminCouponService.couponlist(page,limit,sort,order,couponId,userId,status);
            Object o = ResponseUtil.okList(couponList);
            s = JacksonUtil.getObjectMapper().writeValueAsString(o);
            resp.getWriter().println(s);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private void list(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            String sort = req.getParameter("sort");
            String order = req.getParameter("order");
            String page=req.getParameter("page");
            String limit=req.getParameter("limit");
            String name=req.getParameter("name");
            String type=req.getParameter("type");
            String status=req.getParameter("status");
            List<MarketCoupon> couponList=adminCouponService.list(page, limit, sort, order,name,type,status);
            Object o = ResponseUtil.okList(couponList);
            String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
            resp.getWriter().println(s);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op =requestURI.replace(req.getContextPath()+"/admin/coupon/", "");
        if("create".equals(op)){
            create(req,resp);
        }
        if("update".equals(op)){
            update(req,resp);
        }
        if("delete".equals(op)){
            delete(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String jsonStr =req.getReader().readLine();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            MarketCoupon marketCoupon = objectMapper.readValue(jsonStr, MarketCoupon.class);
            Integer i=adminCouponService.deleteCoupon(marketCoupon);
            if(i>0){
                Object ok = ResponseUtil.ok();
                String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
                resp.getWriter().write(s);
                return;
            }
            Object fail=ResponseUtil.fail();
            String f=JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().write(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            String jsonStr =req.getReader().readLine();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            MarketCoupon marketCoupon = objectMapper.readValue(jsonStr, MarketCoupon.class);
            marketCoupon.setUpdateTime(LocalDateTime.now());
            Integer i=adminCouponService.updateCoupon(marketCoupon);
            if(i>0){
                Object ok = ResponseUtil.ok();
                String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
                resp.getWriter().write(s);
                return;
            }
            Object fail=ResponseUtil.fail();
            String f=JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().write(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void create(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            String jsonStr =req.getReader().readLine();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            MarketCoupon marketCoupon = objectMapper.readValue(jsonStr, MarketCoupon.class);
            marketCoupon.setAddTime(LocalDateTime.now());
            marketCoupon.setUpdateTime(LocalDateTime.now());
            Integer i=adminCouponService.addCoupon(marketCoupon);
            if(i>0){
                Object ok = ResponseUtil.ok();
                String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
                resp.getWriter().write(s);
                return;
            }
            Object fail=ResponseUtil.fail();
            String f=JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().write(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
